package com.qfedu.qfoffice.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.qfoffice.entity.Course;
import com.qfedu.qfoffice.entity.ResponseData;
import com.qfedu.qfoffice.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class CourseController {

    @Autowired
    private CourseService courseService;

    @RequestMapping("/course/coursepage.do")
    @ResponseBody
    public ResponseData<List<Course>> coursepage(int page, int limit) {

        //开启分页
        PageHelper.startPage(page, limit);
        List<Course> courseList = courseService.findAllCourse();
        // 得到分页相关信息
        PageInfo pageInfo = new PageInfo(courseList);
        ResponseData<List<Course>> responseData = new ResponseData<>();
        responseData.setCode(0);
        responseData.setMsg("success");
        responseData.setCount((int) pageInfo.getTotal());
        responseData.setData(courseList);
        return responseData;
    }

    @RequestMapping("/course/coursedelete.do")
    @ResponseBody
    public ResponseData<Course> coursedelete(int id) {
        ResponseData<Course> responseData = new ResponseData<>();
        int i = courseService.deleteCourseById(id);
        if (i > 0)
            responseData.setCode(1000);
        return responseData;
    }


    @RequestMapping("/course/courseupdate.do")
    @ResponseBody
    public ResponseData<Course> courseupdate(int id, HttpServletRequest request) {
        String name = request.getParameter("name");
        String createtime = request.getParameter("createtime");
        String weekStr = request.getParameter("week");
        String typeStr = request.getParameter("type");
        int week = Integer.valueOf(weekStr);
        int type = Integer.valueOf(typeStr);
        ResponseData<Course> responseData = new ResponseData<>();
        Course course = new Course();
        course.setId(id);
        course.setName(name);
        course.setCreatetime(createtime);
        course.setWeek(week);
        course.setType(type);
        int i = courseService.updateCourse(course);
        if (i > 0)
            responseData.setCode(1);
        return responseData;
    }

    @RequestMapping("/course/addCourse.do")
    @ResponseBody
    public ResponseData<Course> addCourse(HttpServletRequest request) {
        String name = request.getParameter("name");
        String createtime = request.getParameter("createtime");
        String weekStr = request.getParameter("week");
        String typeStr = request.getParameter("type");
        int week = Integer.valueOf(weekStr);
        int type = Integer.valueOf(typeStr);
        ResponseData<Course> responseData = new ResponseData<>();
        Course course = new Course();
        course.setName(name);
        course.setCreatetime(createtime);
        course.setWeek(week);
        course.setType(type);
        int i = courseService.addCourse(course);
        if (i > 0)
            responseData.setCode(1);
        return responseData;

    }
}

